package edu.hebeu.evil.freshmenguide;

import android.graphics.Point;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.FrameLayout;

import edu.hebeu.evil.freshmenguide.Fragment.LeftMenuFragment;
import edu.hebeu.evil.freshmenguide.Utils.SuperToast.ProgressToast;
import edu.hebeu.evil.freshmenguide.Utils.SuperToast.SuperToast;
import edu.hebeu.evil.freshmenguide.Utils.Utils;
import edu.hebeu.evil.freshmenguide.View.ScrollViewNextWebView;


public class MainActivity extends BaseActivity
{
    private Toolbar toolbar;

    private WebView webView;
    private FrameLayout container_left;
    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle barDrawerToggle;

    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    private ScrollViewNextWebView scrollView;


    @Override
    public int setContentView()
    {
        return R.layout.activity_main;
    }

    @Override
    public void onCreate()
    {
        initView();
        setDrawerlayout();
        initFragment();
        setCenterBotton();
        setWebView();
        //webView.loadUrl("http://www.baidu.com");
        webView.loadUrl("file:///android_asset/hebeu.html");
        isShowLead();
    }

    /**
     * 初始化控件
     */
    private void initView()
    {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        webView = (WebView) findViewById(R.id.webView);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        container_left = (FrameLayout) findViewById(R.id.container_left);
        scrollView = (ScrollViewNextWebView) findViewById(R.id.scroll);
    }

    /**
     * 是否显示引导
     */
    private void isShowLead()
    {
        //Bundle bundle = getIntent().getBundleExtra("Bundle");
        //boolean isShowLead = bundle.getBoolean("isLead", false);
        //if (isShowLead)
        {
        }
    }

    /**
     * 初始化Fragment
     */
    private void initFragment()
    {
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();

        if (fragmentManager.findFragmentById(R.id.container_left) == null)
            transaction.add(R.id.container_left, LeftMenuFragment.newInstance()).commit();
    }


    /**
     * 设置侧滑栏
     */
    private void setDrawerlayout()
    {
        barDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close)
        {
            /*todo:待定方法，不确定使用*/
            @Override
            public void onDrawerClosed(View drawerView)
            {
                super.onDrawerClosed(drawerView);
                Utils.Log(scrollView.getScrollY());
                if (scrollView.getScrollY() == 720)
                {
                    scrollView.scrollTo(0, 0);
                }
            }
        };
        barDrawerToggle.syncState();
        drawerLayout.addDrawerListener(barDrawerToggle);
    }

    /**
     * 设置中间按钮
     */
    private void setCenterBotton()
    {

    }


    /**
     * 设置WebView
     */
    private void setWebView()
    {
        WebSettings webSettings = webView.getSettings();

        /*设置适应屏幕*/
        {
            webSettings.setUseWideViewPort(true);//设置此属性，可任意比例缩放
            webSettings.setLoadWithOverviewMode(true);
        }
        /*支持缩放*/
        {
            webSettings.setJavaScriptEnabled(true);
            webSettings.setBuiltInZoomControls(true);
            webSettings.setSupportZoom(true);
        }
        /*优先使用缓存*/
        {
            webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            // 开启 DOM storage API 功能
            webSettings.setDomStorageEnabled(true);
        }

        /*在当前页面打开新的链接*/
        webView.setWebViewClient(new WebViewClient()
        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }
        });
    }


    @Override
    public void onBackPressed()
    {
        //super.onBackPressed();
        if (drawerLayout.isDrawerOpen(container_left))
            drawerLayout.closeDrawer(container_left);
        else
            twoClickExit();
    }

    /*********************************************
     * 双击退出
     */
    private long exitTime = 0;
    private ProgressToast progressToast;
    private int progress;

    private void twoClickExit()
    {
        if ((System.currentTimeMillis() - exitTime) > 2000)
        {
            progressToast = SuperToast.progressToast(MainActivity.this)
                    .setText("再次点击退出")
                    .setBackgroundColor(ProgressToast.Background.GRAY)
                    .setAnimations(ProgressToast.Animations.POPUP)
                    .setMaxProgress(200).show();
            new Utils.SHandler()
            {
                @Override
                public void thread(Handler handler)
                {
                    for (progress = 1; progress <= 200; progress++)
                    {
                        handler.sendEmptyMessage(0);
                        Utils.Threadsleep(10);
                    }
                }

                @Override
                public void handler()
                {
                    progressToast.setProgress(progress);
                }
            }.startByCustom();
            exitTime = System.currentTimeMillis();
        }
        else
            finish();
    }
}
